package com.liufh.controller;

import com.liufh.common.ResultData;
import com.liufh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author: liufh
 * @create: 2022-01-21 09:54
 **/
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册
     */
    @PostMapping("/register/{email}/{password}/{code}")
    public ResultData register(@PathVariable("email") String email, @PathVariable("password") String password, @PathVariable("code") String code, HttpServletResponse response) throws Exception {
        System.out.println("====register email:" + email);
        String resCode = userService.register(email, password, code);
        return ResultData.success(response, "ok", "success", resCode);
//        return ResultData.fail(response, "999", "fail", "No");
    }

    /**
     * 是否已注册
     */
    @PostMapping("/isRegistered/{email}")
    public ResultData isRegistered(@PathVariable("email") String email, HttpServletResponse response) throws Exception {
        System.out.println("====isRegistered email:" + email);
        return ResultData.success(response, "ok", "success", "0");
    }

    /**
     * 登录
     */
    @PostMapping("/login/{email}/{password}")
    public ResultData login(@PathVariable("email") String email, @PathVariable("password") String password, HttpServletResponse response) throws Exception {
        System.out.println("====login email:" + email);
        ResultData resultData = userService.login(email, password, response);
        return resultData;
    }

    /**
     * 查询⽤户登录邮箱
     */
    @PostMapping("/info/{token}")
    public ResultData info(@PathVariable("token") String token, HttpServletResponse response) throws Exception {
        System.out.println("====info token:" + token);
        String email = userService.userInfo(token);
        return ResultData.success(response, email, "success", "0");
    }

}
